package com.itheima.controller;


import com.github.pagehelper.Page;
import com.itheima.pojo.Blog;
import com.itheima.pojo.BlogSort;
import com.itheima.service.BlogSortService;
import com.itheima.utils.MessageConstant;
import com.itheima.utils.ResultUtil;
import com.itheima.vo.BlogSortVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/classify")
@Api(tags = "分类相关接口")
@CrossOrigin
public class ClassifyController {
    @Autowired
    BlogSortService blogSortService;

    @GetMapping("/getArticleByBlogSortUid/{blogSortUid}/{currentPage}/{pageSize}")
    @ApiOperation("/通过bloguid获取文章")
    public String getArticleByBlogSortuid(@PathVariable("blogSortUid") String blogSortUid,@PathVariable("currntPage") Integer currentPage,@PathVariable("PageSize") Integer pageSize){
        if(blogSortUid==null||"".equals(blogSortUid)){
            return ResultUtil.result(401, MessageConstant.MISSING_KEY_INFORMATION);
        }
        try{
            Page<Blog> articleByBlogSortUid = blogSortService.getArticleByBlogSortUid(blogSortUid, currentPage, pageSize);
            return ResultUtil.result(200,articleByBlogSortUid);
        } catch (Exception e){
            e.printStackTrace();
            return ResultUtil.result(401,MessageConstant.GET_BLOGSORT_FAIL);
        }
    }

    @GetMapping("/getBlogSortList")
    @ApiOperation("获取分类信息")
    public String getBlogSortList(){
        try{
            BlogSortVO blogSortVO=new BlogSortVO();
            blogSortVO.setCurrentPage(1);
            blogSortVO.setPageSize(10);
            List<BlogSort> blogSortList = blogSortService.getList(blogSortVO);
            return ResultUtil.result(HttpServletResponse.SC_OK,blogSortList);
        } catch (Exception e){
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST,MessageConstant.BLOGSORT_FINDALL_FAIL);
        }
    }

}
